package com.manger.common.office.sax.utils;

import com.manger.common.annotation.excel.Excel;
import com.manger.common.annotation.excel.ExcelTypeMap;
import com.manger.common.office.sax.ExcelDicClass;
import com.manger.model.CaseInfo;
import com.manger.model.Dictionary_Data;
import com.manger.utils.DataTools;
import org.apache.commons.lang.StringUtils;

import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;

public class TargetClassParse<T> {

    public static   List<UserTargetModel> parse(Class<?> claz) {
        List<UserTargetModel> userTargetModels = new ArrayList<>();
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        while (claz != null) {
            Field[] fields = claz.getDeclaredFields();
            if (fields == null || fields.length <= 0) {
                break;
            }
            UserTargetModel userTargetModel = null;
            for (Field field : fields) {
                Excel excel = field.getAnnotation(Excel.class);
                if (excel == null) {
                    continue;
                }

                Map<String, String> typeMap = new LinkedHashMap<>();
                List<String> dropDownList = new ArrayList<>();

                userTargetModel = new UserTargetModel();

                if (excel.dic().typeMap() != null && excel.dic().typeMap().length > 0) {

                    ExcelTypeMap[] excelTypeMap = excel.dic().typeMap();

                    for (ExcelTypeMap type : excelTypeMap) {
                        typeMap.put(type.val(), type.text());
                        dropDownList.add(type.text());
                    }

                }

                if (StringUtils.isNotEmpty(excel.dic().dicType())) {
                    List<Dictionary_Data> dicList = DataTools.getDicDataByGroupIdAndOrder(excel.dic().dicType(),
                            excel.dic().sort().getEnDesc());
                    for (Dictionary_Data dictionaryData : dicList) {
                        typeMap.put(dictionaryData.getVal(), dictionaryData.getText());
                        dropDownList.add(dictionaryData.getText());
                    }
                }
                Class<? extends ExcelDicClass> dicClaz = excel.dic().claz();
                if (dicClaz.getSuperclass() != null && dicClaz.getSuperclass().equals(ExcelDicClass.class)) {
                    try {
                        Object object = dicClaz.newInstance();
                        Method method = dicClaz.getDeclaredMethod("getDic");
                        method.setAccessible(true);
                        Map<String, String> dicMap = (Map<String, String>) method.invoke(object);
                        ;
                        for (String key : dicMap.keySet()) {
                            typeMap.put(key, dicMap.get(key));
                            dropDownList.add(dicMap.get(key));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
                userTargetModel.setField(field);
                userTargetModel.setExcel(excel);
                userTargetModel.setDicMap(typeMap);
                userTargetModel.setDropDownList(dropDownList);

                userTargetModels.add(userTargetModel);
            }
            claz = claz.getSuperclass();
        }
        userTargetModels.sort(new Comparator<UserTargetModel>() {
            @Override
            public int compare(UserTargetModel o1, UserTargetModel o2) {
                return o1.getExcel().export().sort() - o2.getExcel().export().sort();
            }
        });
        return userTargetModels;
    }

    public static UserTargetModel getUserTargetModel(String titleName, List<UserTargetModel> userTargetModelList) {
        for (UserTargetModel userTargetModel : userTargetModelList) {
            if (titleName.equals(userTargetModel.getExcel().desc())) {
                return userTargetModel;
            }
        }
        return null;
    }

    public static void main(String[] args) {
        TargetClassParse.parse(CaseInfo.class);
    }

}
